/**
 * 获取图片宽高
 * @param url 图片路径
 * @returns promise 图片的宽高
 */
export function getImageSize(url: string) {
  return new Promise(function (resolve, reject) {
    let image = new Image();
    image.onload = function () {
      resolve({
        width: image.width,
        height: image.height,
      });
    };
    image.onerror = function () {
      reject(new Error('error'));
    };
    image.src = url;
  });
}
/**
 * 判断是否为json字符串
 * @param str
 * @returns boolean
 */
export function isJSON(str: string) {
  if (typeof str == 'string') {
    try {
      var obj = JSON.parse(str);
      if (typeof obj == 'object' && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      console.log('error：' + str + '!!!' + e);
      return false;
    }
  }
}
export function isFullscreen() {
  return (
    document.fullscreenElement ||
    document.msFullscreenElement ||
    document.mozFullScreenElement ||
    document.webkitFullscreenElement ||
    false
  );
}

export function getUserInfo() {
  let user = window.localStorage.getItem('userInfo') || '';
  if (isJSON(user)) {
    return JSON.parse(user);
  } else {
    return {};
  }
}
